﻿----#include "data\config\guide\LoginOutExpConfig.lua" once
----#include "data\functions\Activity\Common\Consumes.lua" once

MiscsImplement.getLogOutExpMsgFunc = function (sysarg, msgid, packet)
	local type = DataPack.readByte(packet) or 1
	if type == nil then 
		return
	end
	
	-- 角色等级是否达到
	local level =  Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if level < LogOutExp.limitLevel[1] or level > LogOutExp.limitLevel[2] then 
		GetLogoutExpReturn(sysarg, 0)
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.lx0002, LogOutExp.limitLevel[1]), ttFlyTip)
		return 
	end
	
	local var = Actor.getStaticVar(sysarg)
	local logoutTime = var.logoutTime
	if logoutTime == nil then
		GetLogoutExpReturn(sysarg, 0)
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.lx0003, LogOutExp.minTime), ttFlyTip)
		return
	end
	
	-- 计算离线时间
	local logoutHour = math.floor(logoutTime / 3600)
	if logoutHour < LogOutExp.minTime then 
		GetLogoutExpReturn(sysarg, 0)
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.lx0003, LogOutExp.minTime), ttFlyTip)
		return 
	end
	
	if logoutHour > LogOutExp.maxTime then
		logoutHour = LogOutExp.maxTime
	end
	
	local expPerLevel = LogOutExp.expPerLevel
	if expPerLevel.type ~= 20 then
		GetLogoutExpReturn(sysarg, 0)
		print("getLogOutExpMsgFunc award type error");
		return 
	end
	
	local baseExp = LogOutExp.outexp
	if baseExp == nil or baseExp == 0 then 
		GetLogoutExpReturn(sysarg, 0)
		print("getLogOutExpMsgFunc outexp error");
		return 
	end
	
	-- 是否vip
	local vipLevel = Actor.getVipGrade(sysarg)
	if not vipLevel then vipLevel = 0 end
	
	local logoutExp = 0
	local vipLimit = LogOutExp.vipLevelLimit;
	local vipRate = LogOutExp.vipRate
	if vipLevel >= vipRate[type].viplv then
		logoutExp = math.floor(vipRate[type].rate* baseExp *logoutHour)
	else
		GetLogoutExpReturn(sysarg, 0);
		SendRecommendTips(sysarg, PopUpWndType.PayWindow, Lang.ScriptTips.lx0004);
		return;
	end

	var.logoutTime = 0
	
	-- 获得经验奖励
	Actor.addExp(sysarg, logoutExp, 169, 0)
	Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.lx0001, logoutExp), ttFlyTip)
	GetLogoutExpReturn(sysarg, 1)
end

-- 领取离线经验结果
function GetLogoutExpReturn(sysarg, result)
	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetLogoutExpResult)
	if npack == nil then return end
	DataPack.writeByte(npack, result)
	DataPack.flush(npack)
end


MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cLogOutExp, MiscsImplement.getLogOutExpMsgFunc)